package domainModel;

import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;


/**
 * The Class Topic.
 */
public class Topic implements TopicInt {

	/** The counter is a static int that responsible for the auto increment of the id. */
	private static int counter = 0;
	
	/** The id. */
	private int id;
	
	/** The title. */
	private String title;
	
	/** The creation time. */
	private Date creationTime;
	
	private ConcurrentHashMap<Integer, PostInt> posts;
	
	private MemberInt creator;
	/**
	 * Instantiates a new topic.
	 *
	 * @param title the title
	 */
	public Topic(String title, MemberInt creator){
		this.setTitle(title);
		this.setCreationTime();
		this.setId();
		this.posts = new ConcurrentHashMap<Integer, PostInt>();
		this.creator = creator;
	}
	
	/**
	 * Sets the id. it uses the static variable counter in order to make the id unique among all the topic objects.
	 */
	private void setId(){
		this.id = counter;
		counter++;
	}
	
	/**
	 * Sets the creation time. when a topic is constructed its get the time of its construction.
	 */
	private void setCreationTime(){
		this.creationTime = new Date();
	}
	
	/* (non-Javadoc)
	 * @see domainModel.TopicInt#addPost(domainModel.PostInt)
	 */
	@Override
	public void addPost(PostInt newPost) {
		this.posts.put(newPost.getId(), newPost);
	}

	/* (non-Javadoc)
	 * @see domainModel.TopicInt#getCreationTime()
	 */
	@Override
	public Date getCreationTime() {
		return this.creationTime;
	}

	/* (non-Javadoc)
	 * @see domainModel.TopicInt#getId()
	 */
	@Override
	public int getId() {
		return this.id;
	}

	/* (non-Javadoc)
	 * @see domainModel.TopicInt#getTitle()
	 */
	@Override
	public String getTitle() {
		return this.title;
	}

	/* (non-Javadoc)
	 * @see domainModel.TopicInt#removePost(domainModel.PostInt)
	 */
	public void removePost(int postId) {
		this.posts.remove(postId);
	}

	/* (non-Javadoc)
	 * @see domainModel.TopicInt#setTitle(java.lang.String)
	 */
	@Override
	public void setTitle(String title) {
		this.title = title;
	}

	@Override
	public MemberInt getCreator() {
		return this.creator;
	}

	@Override
	public void setCreator(MemberInt creator) {
		this.creator = creator;	
	}

	public ConcurrentHashMap<Integer, PostInt> getPosts()
	{
		return this.posts;
	}
}
